<template>
  <div class="login-container">
    <!-- 标题栏 -->
    <van-nav-bar title="登录" />
    <!-- form表单验证栏 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="用户名"
        left-icon="chat-o"
        :rules="userFormRules.mobile"
      >
        <!--
        利用vant自带的图标
         <i></i> -->
      </van-field>
      <!-- <van-field
        type="password"
        name="密码"
        placeholder="密码"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /^[0-9a-zA-Z]\S{5,10}$/, message: '请输入6到10位的密码' }
        ]"
      > -->
      <!-- 这个是验证表单 -->
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
        type="number"
        maxlength="6"
      >
        <!-- 利用自定义的图标 -->
        <i slot="left-icon" class="iconfont icon-chazhao"></i>

        <template #button>
          <!-- 倒计时 -->
          <van-count-down
            v-if="isCountDownShow"
            slot="button"
            :time="1000 * 5"
            format="ss s"
            @finish="isCountDownShow = false"
          />
          <!-- <van-button
            v-else
            slot="button"
            size="small"
            type="primary"
            round
            @click="onSendSmsCode"
            >发送验证码</van-button
          > -->
          <van-button
            v-else
            native-type="button"
            class="send-code-btn"
            size="mini"
            round
            type="default"
            @click="onSendSmsCode"
          >
            发送验证码
          </van-button>
        </template>
      </van-field>

      <!-- </van-field> -->
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
// 导入login的请求,可以查看请求方式和请求地址
import { login, getsendSms } from '@/api/user.js'
export default {
  name: 'LoginPage',
  components: {},
  props: {},
  data() {
    return {
      // 在 data 中添加数据用来控制倒计时的显示和隐藏
      isCountDownShow: false,
      // 注释
      // username: '',
      // password: ''
      user: {
        // 手机号
        mobile: '13800000001',
        // 验证码
        code: '246810'
      },
      userFormRules: {
        mobile: [
          {
            required: true,
            message: '手机号不能为空'
          },
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: '手机号格式错误'
          }
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空'
          },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式错误'
          }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},

  methods: {
    // 点击登录
    async onSubmit() {
      // 获取表单的值

      const user = this.user
      // console.log(user)

      // 清量加载提示
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true, // 是否禁止背景点击
        duration: 0 // 持续时间，默认 2000，0 表示持续展示不关闭,等待下一个toast触发,当前就会消失
      })

      try {
        // try处理代码的,遇到错误会给到catch的err

        const res = await login(user)
        console.log('登录成功', res)

        // 成功后获取到token,保存到vuex
        this.$store.commit('updatatoken', res.data.data.token)

        // 处理成功后,弹窗输出
        this.$toast.success('登录成功')
      } catch (err) {
        if (err.response.status === 400) {
          // 失败弹框提示
          this.$toast.fail('登录失败')
          console.log('登录失败', err)
        }
      }
    },

    // 点击发送验证码
    async onSendSmsCode() {
      console.log('onSendSmsCode')
      // 1. 校验手机号
      try {
        await this.$refs.loginForm.validate('mobile')
        console.log('验证通过')
      } catch (err) {
        return console.log('验证失败', err)
      }
      // 2. 验证通过，显示倒计时
      this.isCountDownShow = true
      // 3. 请求发送验证码(发送ajax)
      try {
        const res = await getsendSms(this.user.mobile)
        console.log('res', res)
        this.$toast('发送成功')
      } catch (err) {
        // 发送失败，关闭倒计时
        this.isCountDownShow = false
        if (err.response.status === 429) {
          this.$toast('发送太频繁了，请稍后重试')
        } else {
          this.$toast('发送失败，请稍后重试')
        }
      }
    }
  }
}
</script>
<style scoped lang="less"></style>
